package com.liu.file.table;

import java.awt.Color;
import java.awt.FontFormatException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.ResourceBundle;

//import org.apache.struts2.ServletActionContext;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;

/**
 *  将html的style转换成pdf中的style
 */
public class Style {
	private static BaseFont bfChinese ;
	private static Font fontChinese;
	private static Font chinese4 ;
	private static String imgPath;
	public static java.awt.Font MSYH;
	static {
			try {
				String fontPath = ResourceBundle.getBundle("app").getString("font.path");
				MSYH = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new FileInputStream(fontPath));
				bfChinese = BaseFont.createFont(fontPath,
						BaseFont.IDENTITY_H,
						BaseFont.NOT_EMBEDDED);
				fontChinese = new Font(bfChinese, 10, Font.BOLD, new BaseColor(82, 147, 213));
				chinese4 = new Font(bfChinese, 10, Font.NORMAL);
//				imgPath = ReportPieceTable.class.getResource("/").getPath();
//				imgPath =ServletActionContext.getServletContext().getRealPath("/")+"img";
			} catch (DocumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (FontFormatException e) {
				e.printStackTrace();
			}
	}
	
	public static Font getDefaultTHFont(){
		return fontChinese;
	}
	
	public static Font getDefaultTDFont(){
		return chinese4;
	}
	
	public static void addStyle(PdfPCell cell,Map<String, String> styles,boolean isTH,float x,DIV div){
		if(isTH){
			cell.setBorder(0);
			cell.setBorderWidthTop(1.6f);
			cell.setBorderColorTop(new BaseColor(121, 120, 120));
			cell.setBorderWidthBottom(0.5f);
			cell.setBorderColorTop(new BaseColor(121, 120, 120));
			cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		}else{
			cell.setBorder(0);
			cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
			String cssClass = styles.get("cssClass");
			if(cssClass!=null ){
				boolean flag = false;
				Color left=null;
				if(cssClass.contains("Low")){
					flag = true;
					cell.setBorderColorLeft(new BaseColor(104,178,225));
				}else if(cssClass.contains("Middle")){
					flag = true;
					cell.setBorderColorLeft(new BaseColor(251,209,11));
				}else if(cssClass.contains("High")){
					flag = true;
					cell.setBorderColorLeft(new BaseColor(244,143,53));
				}
				if(flag){
					float divWidth = Float.parseFloat(div.getStyle().get("width"));
					float percent = Float.parseFloat(div.getData().replace("%", ""));
					cell.setBorderWidthLeft(divWidth*percent/100);
//					Color white = Color.white;
//					cell.setBorderWidthBottom(1f);
//					cell.setBorderWidthTop(1f);
//					cell.setBorderColorBottom(white);
//					cell.setBorderColorTop(white);
				}
			}
		}
	}
	public static String getImgPath(){
		return imgPath;
	}
}
